<?php

/**
 * Description of backofficeEditDoAction
\* www.teuker.nl - mail@teuker.nl
 * 
 * @author francisc
 */

class backofficeEditDoAction extends sfAction {
    
    public function execute($request) {
        $this->form = new ReservationBackofficeForm();

        if ($this->processBackofficeReservation($request)) {
            SqlEngineCore::get()->update(
                    array('table' => 'reservation',
                          'columns' => array(
                                'restaurant_id' => array('value' => $this->getUser()->getAttribute('current_restaurant_id'), 'type' => PDO::PARAM_INT),
                                'user_id' => $this->getUser()->getAttribute('user_id'),
                                'menu_id' => $this->form->getValue('menu_id'),
                                'created_at' => 'NOW()',
                                'date' => $this->form->getValue('date'),
                                'occasion' => $this->form->getValue('occasion'),
                                'time' => $this->form->getValue('time_hour') . ':' . $this->form->getValue('time_minute'),
                                'person_no' => array('value' => $this->form->getValue('person_no'), 'type' => PDO::PARAM_INT),
                                'salutation' => array('value' => $this->form->getValue('salutation'), 'type' => PDO::PARAM_INT),
                                'name_last' => $this->form->getValue('name_last'),
                                'name_first' => $this->form->getValue('name_first'),
                                'name_company' => $this->form->getValue('name_company'),
                                'address' => $this->form->getValue('address'),
                                'phone' => $this->form->getValue('phone'),
                                'mobile' => $this->form->getValue('mobile'),
                                'email' => $this->form->getValue('email'),
                                'comments_internal' => $this->form->getValue('comments_internal'),
                                'comments_diet' => $this->form->getValue('comments_diet'),
                                'comments_general' => $this->form->getValue('comments_general')),
                          'where' => array(
                                'reservation_id' => $request->getParameter('id'))));

            /* update the reservation log */
            $message = __('update') . ' - ' . __('the reservation info has been updated');
            SqlEngineCore::get()->insert(array('table' => 'reservation_log',
                                               'columns' => array(
                                                   'reservation_id' => $request->getParameter('id'),
                                                   'user_id' => $this->getUser()->getAttribute('user_id'),
                                                   'message' => $message,
                                                   'created_at' => 'NOW()')));
            
            $this->redirect('@reservation_list?date=' . $this->form->getValue('date'));
        }
        else {
            $this->setTemplate('backofficeEdit');
        }
    }

    protected function processBackofficeReservation($request) {
        $params = $request->getParameter($this->form->getName());
        $params['date'] = Util::convertDateClientServer($params['date'], 'nl');

        $this->form->bind($params);

        return $this->form->isValid();
    }
}
